
#include"imagelist.h"
#include <QDir>
#include <QElapsedTimer>
#include <QCoreApplication>
#include <QFileDialog>
//QList<ImageBase> ImageList::m_imagelist;
//ImageBase ImageList::m_imagelist[30];
void ImageList::init()
{
  //  for(int i=0;i<30;i++)
  //  {
  //      ImageBase aimage;
  //      m_imagelist.append(aimage);
  //  }
}
ImageList::ImageList()
{
    if(false==m_bini)
    {
        m_bini=true;
        init();
    }
}
ImageList::~ImageList()
{
   // int isize=m_imagelist.size();


}
void ImageList::setimagelist(int inum)
{
   // m_imagelist.clear();
   // for(int i=0;i<inum;i++)
   // {
   //     ImageBase aimage;
   //     m_imagelist.append(aimage);
   //     m_itrglist.append(0);
   // }
}
int ImageList::gettrg(int inum)
{
    int isize=m_itrglist.size();
    if(inum<isize)
    {
       int itrg = m_itrglist[inum];
       if(itrg>0)
       {
         settrg(inum,0);
          return  itrg;
       }
    }
    return  0;
}
int ImageList::waittrg(int inum,int isleeptime)
{
    QElapsedTimer time;
    time.restart();
    while (time.elapsed() < isleeptime)
    {
        if(gettrg(inum) > 0)
            return 1;
        QCoreApplication::processEvents(QEventLoop::AllEvents, 30);
    }
     return 0;
}
void ImageList::settrg(int inum,int ival)
{
    int isize=m_itrglist.size();
    if(inum<isize)
    {
        m_itrglist[inum]=ival;
    }
}
ImageBase * ImageList::getimage(int inum)
{
    int isize = IMAGELISTNUM;// m_imagelist.size();
    if(inum>=0&&inum<isize)
    {
        return &m_imagelist[inum];
    }

    return nullptr;
}
int ImageList::nextcurimgenum()
{
    int inum= m_icurimgnum;
    int isize = IMAGELISTNUM;//m_imagelist.size();
      if(inum>=0&&inum<isize-1)
            m_icurimgnum=m_icurimgnum+1;
      else
          m_icurimgnum =0;
    return inum;
}

ImageBase * ImageList::nextimage()
{

        return &m_imagelist[nextcurimgenum()];
}


void mkdir(const QString &astr)
{
    const QDir cxDir(astr);
    if (!cxDir.exists())
    {
       if (!cxDir.mkpath(astr))
       {
          return ;
       }
    }
}
void ImageList::savex()
{
#if defined( Q_OS_WIN )
    QSize imagesize(2448,2048);
    ImageBase aimage0(imagesize, QImage::Format_ARGB32_Premultiplied);
     int isize = IMAGELISTNUM;
        mkdir(QString("C://CxVision"));
        mkdir(QString("C://CxVision//img"));
        for(int i=0;i<isize;i++)
        {
            QString strname =  QString("C://CxVision//img//%1.jpg").arg(i);

            aimage0.SetMode(0);
            aimage0.ImageAnd(&(m_imagelist[i]));
            aimage0.save(strname);
        }
 #else

    int isize = IMAGELISTNUM;//m_imagelist.size();
       mkdir(QString("/media/cxy/Q/img"));
       for(int i=0;i<isize;i++)
       {
           QString strname =  QString("/media/cxy/Q/img/%1.jpg").arg(i);
           m_imagelist[i].save(strname);
       }
 #endif
}

void ImageList::save()
{
#if defined( Q_OS_WIN )
     int isize = IMAGELISTNUM;
        mkdir(QString("C://CxVision"));
        mkdir(QString("C://CxVision//img"));
        for(int i=0;i<isize;i++)
        {
            QString strname =  QString("C://CxVision//img//%1.jpg").arg(i);
            m_imagelist[i].save(strname);
        }
 #else
    int isize = IMAGELISTNUM;//m_imagelist.size();
       mkdir(QString("/media/cxy/Q/img"));
       for(int i=0;i<isize;i++)
       {
           QString strname =  QString("/media/cxy/Q/img/%1.jpg").arg(i);
           m_imagelist[i].save(strname);
       }
 #endif
}
void ImageList::load()
{
#if defined( Q_OS_WIN )
     int isize =IMAGELISTNUM;
        for(int i=0;i<isize;i++)
        {
            QString strname =  QString("C://CxVision//img//%1.jpg").arg(i);
            m_imagelist[i].load(strname);
        }
#else
    int isize = IMAGELISTNUM;//m_imagelist.size();
       for(int i=0;i<isize;i++)
       {
           QString strname =  QString("/media/cxy/Q/img/%1.jpg").arg(i);
           m_imagelist[i].load(strname);
       }
#endif
}
void ImageList::loadlist()
{
  QString fileName =
  QFileDialog::getOpenFileName(nullptr, QString("Open File"),
                                 QDir::currentPath(),
                                 QString("ALL Files (*.*)"));
  if (fileName.isEmpty())
    return;
  QFileInfo aqfi(fileName);
  QString qfiledir = aqfi.path();
  QString qsuffix=aqfi.suffix();
  int isize = IMAGELISTNUM;//m_imagelist.size();
  for(int i=0;i<isize;i++)
  {
      QString strname = qfiledir+ QString("/%1.").arg(i)+qsuffix;
      m_imagelist[i].load(strname);
  }
}
void ImageList::loadcalibx(const char *pfilename)
{
    QString strloadfile = QString(pfilename);

    QSize imagesize(1280,853);
    QImage aimagex(imagesize, QImage::Format_ARGB32_Premultiplied);
    int inum = 0;
    for(int i=0;i<=160;i=i+8)
    {
        QString strfile =strloadfile+ QString("\\calib%1").arg(i);
        aimagex.load(strfile+QString(".x.bmp"));
        for(int x(0); x <1280; x++)
        {
            for(int y(0); y <  853; y++)
            {
                QRgb icolorx = aimagex.pixel(x,y);
                 m_imagelist[inum].setPixel(x,y,icolorx);
            }
        }
        inum = inum + 1;
    }

}

void ImageList::loadcaliby(const char *pfilename)
{
    QString strloadfile = QString(pfilename);

    QSize imagesize(1280,853);
    QImage aimagey(imagesize, QImage::Format_ARGB32_Premultiplied);
    int inum = 0;
    for(int i=0;i<=160;i=i+8)
    {
        QString strfile =strloadfile+ QString("\\calib%1").arg(i);
        aimagey.load(strfile+QString(".y.bmp"));

        for(int x(0); x <1280; x++)
        {
            for(int y(0); y <  853; y++)
            {
                QRgb icolory = aimagey.pixel(x,y);
                m_imagelist[inum].setPixel(x,y,icolory);
            }
        }
        inum = inum + 1;
    }

}


void ImageList::loadcalibz(const char *pfilename)
{
    QString strloadfile = QString(pfilename);

    QSize imagesize(1280,853);
    QImage aimagez(imagesize, QImage::Format_ARGB32_Premultiplied);
    int inum = 0;
    for(int i=0;i<=160;i=i+8)
    {
        QString strfile =strloadfile+ QString("\\calibz%1").arg(i);
        aimagez.load(strfile+QString(".z.bmp"));
        for(int x(0); x <1280; x++)
        {
            for(int y(0); y <  853; y++)
            {
                QRgb icolorx = aimagez.pixel(x,y);
               m_imagelist[inum].setPixel(x,y,icolorx);
            }
        }

        inum = inum + 1;
    }

}

void ImageList::settoimage(void *pimage)
{
    ImageBase *paimage=(ImageBase *)pimage;
    ImageBase *pbimage=getimage(m_icurimgnum);
    if(nullptr!=pbimage&&nullptr!=paimage)
    {
        paimage->SetMode(0);
        paimage->ImageAnd((void *)pbimage);
    }
}
void ImageList::clearimage()
{
    int isize=IMAGELISTNUM;//m_imagelist.size();
    for(int i=0;i<isize;i++)
    {
        m_imagelist[i].ImageClear();
    }
}


int ImageList::getposvaluex(int ix,int iy)
{
    QColor icolorx = m_imagelist[m_icurimage].pixel(ix,iy);

    int ir0 = icolorx.red();
    int ig0 = icolorx.green();
    int ib0 = icolorx.blue();
    int ixv = (ir0<<16)+(ig0<<8)+ib0;
    return ixv;
}
int ImageList::getposvaluey(int ix,int iy)
{
    QColor icolory = m_imagelist[m_icurimage].pixel(ix,iy);

    int ir0 = icolory.red();
    int ig0 = icolory.green();
    int ib0 = icolory.blue();
    int iyv = (ir0<<16)+(ig0<<8)+ib0;
     return iyv;
}
int ImageList::getposvaluez(int ix,int iy)
{
    QColor icolorz = m_imagelist[m_icurimage].pixel(ix,iy);

    int ir0 = icolorz.red();
    int ig0 = icolorz.green();
    int ib0 = icolorz.blue();
    int zvalue = (ir0<<16)+(ig0<<8)+ib0;
     return zvalue;
}
void ImageList::highvisionvalue(int ix,int iy,int iw)
{
    int ivalue = 0;
    int inextvalue =0;
    for (int i=0;i+1<size();i++)
    {
        QColor icolorz0 = m_imagelist[i].pixel(ix,iy);

        int ir0 = icolorz0.red();
        int ig0 = icolorz0.green();
        int ib0 = icolorz0.blue();
        int zvalue0 = (ir0<<16)+(ig0<<8)+ib0;

        QColor icolorz1 = m_imagelist[i+1].pixel(ix,iy);

        int ir1 = icolorz1.red();
        int ig1 = icolorz1.green();
        int ib1 = icolorz1.blue();
        int zvalue1 = (ir1<<16)+(ig1<<8)+ib1;

        if(i==0&&iw<zvalue0)
        {
           m_icurimage =  0;
            return;
        }
        if(iw<=zvalue1&&iw>=zvalue0)
        {
           m_icurimage = i;
            return;
        }
        if(iw>zvalue0)
            m_icurimage = i;
    }
}
